package yang.yu.sunny.domain;

import org.dayatang.domain.EntityRepository;
import org.dayatang.domain.InstanceFactory;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 产品类别领域服务
 * Created by yyang on 16/3/23.
 */
class ProductCategoryService {

    private List<ProductCategoryRelation> relations;

    private List<ProductCategoryRelation> getRelations() {
        if (relations == null) {
            relations = InstanceFactory.getInstance(EntityRepository.class)
                    .findAll(ProductCategoryRelation.class);
        }
        return relations;
    }


    Optional<ProductCategory> getParentOf(ProductCategory category) {
        return getRelations().stream().filter(it -> it.getChild().equals(category))
                .map(ProductCategoryRelation::getParent)
                .findFirst();
    }

    List<ProductCategory> getChildrenOf(ProductCategory category) {
        return getRelations().stream().filter(it -> it.getParent().equals(category))
                .map(ProductCategoryRelation::getChild)
                .collect(Collectors.toList());
    }

}
